package com.yxh.www.author.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxh.www.author.domain.SmRoleResourceRelation;
import com.yxh.www.author.mapper.SmRoleResourceRelationMapper;
import com.yxh.www.author.service.SmRoleResourceRelationService;
import com.yxh.www.common.result.Result;
import com.yxh.www.common.result.ResultBuilder;
import com.yxh.www.common.result.ResultEnum;
import com.yxh.www.mysql.base.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 系统角色资源关系 服务实现类
 * </p>
 *
 * @author Yangxiaohui
 * @since 2020-05-09
 */
@Service
public class SmRoleResourceRelationServiceImpl extends BaseServiceImpl<SmRoleResourceRelationMapper, SmRoleResourceRelation> implements SmRoleResourceRelationService {

    /**
     * 新增角色资源关系
     *
     * @param roleId      角色ID
     * @param resourceIds 资源ID集合
     * @return 统一响应 {@link Result}
     */
    @Override
    public Result<Object> addSmRoleResourceRelation(String roleId, String[] resourceIds) {
        if (resourceIds.length==0){
            return ResultBuilder.error(ResultEnum.USER_HANDEL_ERROR,"请输入正确的资源ID集合！");
        }
        // 定义插入实体集合
        List<SmRoleResourceRelation> smRoleResourceRelationList=new LinkedList<>();
        // 获取当前时间
        LocalDateTime nowTime=LocalDateTime.now();
        for (String resourceId:resourceIds){
            smRoleResourceRelationList.add(new SmRoleResourceRelation(roleId,resourceId).setCreateTime(nowTime));
        }
        // 持久化
        baseMapper.insertList(smRoleResourceRelationList);
        return ResultBuilder.success();
    }

    /**
     * 根据角色资源关系ID删除关系
     *
     * @param ids 关系ID集合
     * @return 统一响应 {@link Result}
     */
    @Override
    public Result<Object> removeSmRoleResourceRelation(String[] ids) {
        baseMapper.delete(new QueryWrapper<SmRoleResourceRelation>().in("id",ids));
        return ResultBuilder.success();
    }

    /**
     * 根据角色ID删除关系
     *
     * @param roleId 角色ID
     * @return 统一响应 {@link Result}
     */
    @Override
    public Result<Object> removeSmRoleResourceRelationByRoleId(String roleId) {
        baseMapper.delete(new QueryWrapper<SmRoleResourceRelation>().eq("role_id",roleId));
        return ResultBuilder.success();
    }

    /**
     * 根据资源ID删除关系
     *
     * @param resourceId 资源ID
     * @return 统一响应 {@link Result}
     */
    @Override
    public Result<Object> removeSmRoleResourceRelationByResourceId(String resourceId) {
        baseMapper.delete(new QueryWrapper<SmRoleResourceRelation>().eq("resource_id",resourceId));
        return ResultBuilder.success();
    }

    /**
     * 根据角色ID资源ID删除关系
     *
     * @param roleId     角色ID
     * @param resourceId 资源ID
     * @return 统一响应 {@link Result}
     */
    @Override
    public Result<Object> removeSmRoleResourceRelationByRoleIdAndResourceId(String roleId, String resourceId) {
        baseMapper.delete(new QueryWrapper<SmRoleResourceRelation>()
                .eq("role_id",roleId)
                .eq("resource_id",resourceId)
        );
        return ResultBuilder.success();
    }
}
